﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.CarInsurance;

public partial class Provider_CarInsurance_AddNewHSBCInsurance : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICT";
    //public static Int32 _providercarinsid = 0;
    public static Int64 _baseid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            DataTable dtConverageType = General.GetComboItem("CoverageType", false);
            ddlCXCoverageType.DataSource = dtConverageType;
            ddlCXCoverageType.DataTextField = dtConverageType.Columns[0].ColumnName.ToString();
            ddlCXCoverageType.DataValueField = dtConverageType.Columns[1].ColumnName.ToString();
            ddlCXCoverageType.DataBind();
            ddlCXCoverageType.Items.Insert(0, new ListItem("Select", "-1"));
            dtConverageType = null;

            GetCoverageType();
            GetPolicyDoc();
            GetPromotion();
            GetPolicyInfo();

            if (Request.Params["CXCoverTypeId"] != null)
            {
                int _cxcovertypeid = 0;
                if (int.TryParse(Request.Params["CXCoverTypeId"].ToString(), out _cxcovertypeid))
                {
                    ddlCXCoverageType.Text = _cxcovertypeid.ToString();
                    ddlCXCoverageType_SelectedIndexChanged(this, null);
                    //ShowProviderCoverType(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), _cxcovertypeid);
                }
            }



            txtPolicyHolder.Attributes.Add("onkeyup", "GetPolicy()");
            txtPassengers.Attributes.Add("onkeyup", "GetPassengers()");
            txtMedicalExp.Attributes.Add("onkeyup", "GetMedicalExp()");
            txtCashPerDay.Attributes.Add("onkeyup", "GetCashAllowance()");
            txtCashDays.Attributes.Add("onkeyup", "GetCashAllowance()");
            txtCourtesyCar.Attributes.Add("onkeyup", "GetCarReplacement()");
            txtCourtesyCar.Attributes.Add("onClick", "GetCarReplacement()");

            foreach (ListItem item in this.rdoNCD.Items)
            {

                item.Attributes.Add("onClick", "javascript:GetNCD(this);");
            }

            cbIsPersonalAccident.Attributes.Add("onClick", "javascript:PersonalAccidentApplicable(this);");
            cbIsMedicalExp.Attributes.Add("onClick", "javascript:MedicalExpenseApplicable(this);");
            cbIsCashAllowance.Attributes.Add("onClick", "javascript:CashAllowanceApplicable(this);");
            cbIsCourtesyCar.Attributes.Add("onClick", "javascript:CourtesyCarApplicable(this);");
            cbIsPersonalEffect.Attributes.Add("onClick", "javascript:PersonalEffectApplicable(this);");


            if (Request.Params["CXCoverTypeId"] != null)
            {
                Int32 _covertypeid;
                if (Int32.TryParse(Request.Params["CXCoverTypeId"].ToString(), out _covertypeid))
                {
                    ShowCarInsurance(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), _covertypeid);
                }
            }
        }
    }

    private void GetCoverageType()
    {
        CarCoverTypeList _carcovertypelist = new CarCoverTypeList();
        CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

        //DataTable dtConverageType =  General.GetComboItem("CoverageType", false);
        _carcovertypelist = _carcovertypedb.GetProviderCoverTypeList(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));

        if (_carcovertypelist != null)
        {
            for (int i = 0; i < _carcovertypelist.Count; i++)
            {
                //rdoPlanType.Items.Add(new ListItem(_carcovertypelist[i].ProviderCoverTypeDescription.ToString(), _carcovertypelist[i].CXCoverTypeId.ToString()));
            }
        }

        //rdoPlanType.Items.Insert(_carcovertypelist.Count, new ListItem("Any", "0"));
        //rdoPlanType.Text = "-1";


    }

    private void GetPolicyDoc()
    {
        ProviderPolicyDocumentList _policydoclist = new ProviderPolicyDocumentList();
        ProviderPolicyDocumentDB _policydb = new ProviderPolicyDocumentDB();

        _policydoclist = _policydb.GetProviderPolicyDocumentList(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), "CR");

        if (_policydoclist != null)
        {
            ddlPolicyDoc.DataSource = _policydoclist;
            ddlPolicyDoc.DataTextField = "DocumentName";
            ddlPolicyDoc.DataValueField = "Documentid";
            ddlPolicyDoc.DataBind();
        }

        ddlPolicyDoc.Items.Insert(0, new ListItem("Select", "-1"));

    }

    private void GetPromotion()
    {
        CarPromotionList _carpromolist = new CarPromotionList();
        CarPromotionDB _carpromodb = new CarPromotionDB();

        _carpromolist = _carpromodb.GetProviderCarPromotionList(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));

        if (_carpromolist != null)
        {
            ddlPromotion.DataSource = _carpromolist;
            ddlPromotion.DataTextField = "PromotionTitle";
            ddlPromotion.DataValueField = "PromotionId";
            ddlPromotion.DataBind();
        }

        ddlPromotion.Items.Insert(0, new ListItem("Select", "-1"));

    }

    private void GetPolicyInfo()
    {
        CarPolicyInfoList _carpolicylist = new CarPolicyInfoList();
        CarPolicyInfoDB _carpolicyinfodb = new CarPolicyInfoDB();

        _carpolicylist = _carpolicyinfodb.GetProviderCarPolicyInfoList(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));

        if (_carpolicylist != null)
        {
            ddlPolicyInfo.DataSource = _carpolicylist;
            ddlPolicyInfo.DataTextField = "PolicyInfoTitle";
            ddlPolicyInfo.DataValueField = "ProviderCarPolicyId";
            ddlPolicyInfo.DataBind();
        }

        ddlPolicyInfo.Items.Insert(0, new ListItem("Select", "-1"));

    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        IsEnabled();
        if (Page.IsValid)
        {
            //update covertype
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarCoverType _carcovertype = new CarCoverType();
                CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

                _carcovertype.ProviderCoverTypeId = CheckProviderCoverType(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXCoverageType.Text));
                int _covertrantype = 1;
                if (_carcovertype.ProviderCoverTypeId > 0)
                {
                    _covertrantype = 2;
                }
                _carcovertype.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                _carcovertype.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

                _carcovertype.ProviderCoverTypeDescription = txtYourCoverageType.Text;
                _carcovertype.CoverRate = Convert.ToSingle(txtCoverageRate.Text);
                _carcovertype.CXCoverTypeId = Convert.ToInt32(ddlCXCoverageType.Text);
                _carcovertype.MinPremiumAmount = Convert.ToSingle(txtMinimumPremium.Text);
                _carcovertype.MaxPremiumAmount = Convert.ToSingle(txtMaximumPremium.Text);
                _carcovertype.Status = cbStatus.Checked;

                _carcovertype.ProviderCoverTypeId = _carcovertypedb.Insert(_carcovertype);
                InsertProviderAuthTransaction(_carcovertype.ProviderCoverTypeId, "CICarCoverType", _covertrantype, 100, _authcode, _carcovertype.ProviderCompanyId, _carcovertype.RefCEUserId);
                

                if (_carcovertype.ProviderCoverTypeId > 0)
                {
                    //saves the car insurance
                    Int64 newId = 0;
                    CarInsurance _carinsurance = new CarInsurance();
                    CarInsuranceDB _carinsurancedb = new CarInsuranceDB();

                    //_carinsurance.CarInsId = _providercarinsid;
                    _carinsurance.CarInsId = Convert.ToInt32(lblCarIns.Text);
                    int _trantype = 1;
                    if (_carinsurance.CarInsId > 0)
                    {
                        _trantype = 2;
                    }
                    _carinsurance.CIVariableId = 0;
                    _carinsurance.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
                    _carinsurance.CXCoverTypeId = Convert.ToInt32(ddlCXCoverageType.Text);
                    _carinsurance.PolicyName = txtYourCoverageType.Text;
                    _carinsurance.TypeofPlan = txtWorkShop.Text; //ddlCXCoverageType.SelectedItem.ToString();
                    _carinsurance.AnnualPremium = 0;
                    _carinsurance.ProposerExcess = 0;
                    _carinsurance.isPersonalAccident = (cbIsPersonalAccident.Checked == true ? false : true);
                    _carinsurance.PolicyHolderAmt = float.Parse(txtPolicyHolder.Text.Trim());
                    _carinsurance.EachPassengerAmt = float.Parse(txtPassengers.Text.Trim());
                    _carinsurance.PersonalAccAccumulated = float.Parse(txtAccumulated.Text.Trim());

                    _carinsurance.isMedicalExpense = (cbIsMedicalExp.Checked == true ? false : true);
                    _carinsurance.MedicalExpense = float.Parse(txtMedicalExp.Text.Trim());
                    _carinsurance.isCashAllowance = (cbIsCashAllowance.Checked == true ? false : true);
                    _carinsurance.CashAllowance = float.Parse(txtCashPerDay.Text.Trim());

                    _carinsurance.MaxCashAllowanceDays = Convert.ToInt32(txtCashDays.Text.Trim());
                    _carinsurance.IsCarReplacement = (cbIsCourtesyCar.Checked == true ? false : true);
                    _carinsurance.MaxCarReplacementDays = Convert.ToInt32(txtCourtesyCar.Text.Trim());
                    _carinsurance.isPersonnalEffectsLoss = (cbIsPersonalEffect.Checked == true ? false : true);
                    _carinsurance.PersonnalEffectLoss = float.Parse(txtPEffect.Text.Trim());
                    _carinsurance.IsNCDProtection = Convert.ToBoolean(rdoNCD.SelectedValue);
                    _carinsurance.MoreInfo = "";
                    _carinsurance.Status = cbStatus.Checked;

                    _carinsurance.Ninfo1 = Convert.ToInt64(ddlPolicyDoc.SelectedValue);
                    _carinsurance.Ninfo2 = Convert.ToInt64(ddlPromotion.SelectedValue);
                    _carinsurance.Ninfo3 = Convert.ToInt64(ddlPolicyInfo.SelectedValue);

                    newId = _carinsurancedb.Insert(_carinsurance);

                    if (newId > 0)
                    {
                        InsertProviderAuthTransaction(_carinsurance.CarInsId, "CICarInsurance", _trantype, 100, _authcode, _carinsurance.ProviderCompanyId, _carcovertype.RefCEUserId);
                        
                        lblMessage.Text = "Saved successfully!";
                        Response.Redirect("ViewProviderCarCoverTypeHSBC.aspx");
                    }
                    else
                        lblMessage.Text = "Saving failed!";

                }

            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
       
    }

    protected Int64 CheckProviderCoverType(Int64 ProviderCompanyId, Int32 CXCoverTypeId)
    {
        CarCoverType _carcovertype = new CarCoverType();
        CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();
        Int64 _retValue = 0;
        _carcovertype = _carcovertypedb.GetProviderCoverType(ProviderCompanyId, CXCoverTypeId);
        if (_carcovertype != null)
        {
            _retValue = _carcovertype.ProviderCoverTypeId;
        }
        return _retValue;
    }

    protected void ShowProviderCoverType(Int64 ProviderCompanyId, Int32 CXCoverTypeId)
    {
        CarCoverType _carcovertype = new CarCoverType();
        CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

        _carcovertype = _carcovertypedb.GetProviderCoverType(ProviderCompanyId, CXCoverTypeId);
        if (_carcovertype != null)
        {
            txtYourCoverageType.Text = _carcovertype.ProviderCoverTypeDescription;
            txtCoverageRate.Text = _carcovertype.CoverRate.ToString();
            txtMinimumPremium.Text = _carcovertype.MinPremiumAmount.ToString();
            txtMaximumPremium.Text = _carcovertype.MaxPremiumAmount.ToString();
        }
        else
        {
            txtYourCoverageType.Text = "";
            txtCoverageRate.Text = "";
            txtMinimumPremium.Text = "";
            txtMaximumPremium.Text = "";
        }

        //Gets the Base Value

        //CarBaseValue _carbasevalue = new CarBaseValue();
        //CarBaseValueDB _carbasevaluedb = new CarBaseValueDB();

        //_carbasevalue = _carbasevaluedb.GetCarBaseValue(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), CXCoverTypeId);

        //if (_carbasevalue != null)
        //{
        //    _baseid = _carbasevalue.BaseId;
        //    txtBaseValue.Text = _carbasevalue.BaseValue.ToString();
        //}


    }

    private void ShowCarInsurance(Int64 ProviderCompanyId, Int32 CXCoverTypeId)
    {
        CarInsurance _carinsurance = new CarInsurance();
        CarInsuranceDB _carinsurancedb = new CarInsuranceDB();

        _carinsurance = _carinsurancedb.GetCarInsuranceItemByCover(ProviderCompanyId, CXCoverTypeId);

        //_providercarinsid = 0;
        lblCarIns.Text = "0";

        if (_carinsurance != null)
        {
            //_providercarinsid = _carinsurance.CarInsId;
            lblCarIns.Text = _carinsurance.CarInsId.ToString();
            txtYourCoverageType.Text = _carinsurance.PolicyName;
            txtWorkShop.Text = _carinsurance.TypeofPlan;

            cbIsPersonalAccident.Checked = _carinsurance.isPersonalAccident == true ? false : true;
            txtPolicyHolder.Text = _carinsurance.PolicyHolderAmt.ToString();
            txtPassengers.Text = _carinsurance.EachPassengerAmt.ToString();
            txtAccumulated.Text = _carinsurance.PersonalAccAccumulated.ToString();

            cbIsMedicalExp.Checked = _carinsurance.isMedicalExpense == true ? false : true;
            txtMedicalExp.Text = _carinsurance.MedicalExpense.ToString();
            cbIsCashAllowance.Checked = _carinsurance.isCashAllowance == true ? false : true;
            txtCashPerDay.Text = _carinsurance.CashAllowance.ToString();
            txtCashDays.Text = _carinsurance.MaxCashAllowanceDays.ToString();
            txtCourtesyCar.Text = _carinsurance.MaxCarReplacementDays.ToString();
            cbIsCourtesyCar.Checked = _carinsurance.IsCarReplacement == true ? false : true;

            cbIsPersonalEffect.Checked = _carinsurance.isPersonnalEffectsLoss == true ? false : true;
            txtPEffect.Text = _carinsurance.PersonnalEffectLoss.ToString();
            rdoNCD.SelectedValue = _carinsurance.IsNCDProtection.ToString();
            cbStatus.Checked = _carinsurance.Status;

            //preview
            lblPolicy.Value = _carinsurance.PolicyHolderAmt.ToString();
            lblPassengers.Value = _carinsurance.EachPassengerAmt.ToString();
            lblMedicalExp.Value = _carinsurance.MedicalExpense.ToString();
            lblCashAllowance.Value = "$" + _carinsurance.CashAllowance.ToString() + "/day max " + _carinsurance.MaxCashAllowanceDays.ToString() + " days";
            lblCarReplacement.Value = _carinsurance.MaxCarReplacementDays.ToString();
            //lblWorkshop.Value = _carinsurance.TypeofPlan.ToString();
            lblNCD.Value = rdoNCD.SelectedItem.ToString();

            ddlPolicyDoc.SelectedValue = _carinsurance.Ninfo1.ToString();
            ddlPromotion.SelectedValue = _carinsurance.Ninfo2.ToString();
            ddlPolicyInfo.SelectedValue = _carinsurance.Ninfo3.ToString();

        }

        IsEnabled();

    }

    public void IsEnabled()
    {

        if (cbIsPersonalAccident.Checked)
        {
            txtPolicyHolder.Enabled = false;
            txtPassengers.Enabled = false;
            txtAccumulated.Enabled = false;
            rfvPolicyHolder.Enabled = false;
            revPolicyHolder.Enabled = false;
            rfvPassengers.Enabled = false;
            rfvtxtAccumulated.Enabled = false;
            revtxtAccumulated.Enabled = false;
            txtPolicyHolder.Text = "0";
            txtPassengers.Text = "0";
            txtAccumulated.Text = "0";

        }
        else
        {
            txtPolicyHolder.Enabled = true;
            txtPassengers.Enabled = true;
            txtAccumulated.Enabled = true;
            rfvPolicyHolder.Enabled = true;
            revPolicyHolder.Enabled = true;
            rfvPassengers.Enabled = true;
            rfvtxtAccumulated.Enabled = true;
            revtxtAccumulated.Enabled = true;
        }

        if (cbIsMedicalExp.Checked)
        {
            txtMedicalExp.Enabled = false;
            rfvMedicalExp.Enabled = false;
            revMedicalExp.Enabled = false;
            txtMedicalExp.Text = "0";
        }
        else
        {
            txtMedicalExp.Enabled = true;
            rfvMedicalExp.Enabled = true;
            revMedicalExp.Enabled = true;
        }

        if (cbIsCashAllowance.Checked)
        {
            txtCashDays.Enabled = false;
            txtCashPerDay.Enabled = false;
            revCashDays.Enabled = false;
            rfvCashDays.Enabled = false;
            rfvCashPerDay.Enabled = false;
            revCashPerDay.Enabled = false;
            txtCashDays.Text = "0";
            txtCashPerDay.Text = "0";
        }
        else
        {
            txtCashDays.Enabled = true;
            txtCashPerDay.Enabled = true;
            revCashDays.Enabled = true;
            rfvCashDays.Enabled = true;
            rfvCashPerDay.Enabled = true;
            revCashPerDay.Enabled = true;
        }

        if (cbIsCourtesyCar.Checked)
        {
            txtCourtesyCar.Enabled = false;
            rfvCourtesyCar.Enabled = false;
            revCourtesyCar.Enabled = false;
            txtCourtesyCar.Text = "0";
        }
        else
        {
            txtCourtesyCar.Enabled = true;
            rfvCourtesyCar.Enabled = true;
            revCourtesyCar.Enabled = true;
        }

        if (cbIsPersonalEffect.Checked)
        {
            txtPEffect.Enabled = false;
            rfvPEffect.Enabled = false;
            revPEffect.Enabled = false;
            txtPEffect.Text = "0";

        }
        else
        {
            txtPEffect.Enabled = true;
            rfvPEffect.Enabled = true;
            revPEffect.Enabled = true;
        }
    }

    protected void ClearFields()
    {   
        txtMaximumPremium.Text = "";
        txtMinimumPremium.Text = "";
        txtCoverageRate.Text = "";
        txtYourCoverageType.Text = "";
        txtWorkShop.Text = "";

        txtAccumulated.Text = "";
        txtCashDays.Text = "";
        txtCashPerDay.Text = "";
        txtCourtesyCar.Text = "";
        txtMedicalExp.Text = "";
        txtPassengers.Text = "";
        txtPEffect.Text = "";
        txtPolicyHolder.Text = "";
        //txtPolicyName.Text = "";
        cbIsCashAllowance.Checked = false;
        cbIsCourtesyCar.Checked = false;
        cbIsMedicalExp.Checked = false;
        cbIsPersonalAccident.Checked = false;
        cbIsPersonalEffect.Checked = false;
        cbStatus.Checked = false;

        lblCarReplacement.Value = "";
        lblCashAllowance.Value = "";
        lblMedicalExp.Value = "";
        lblNCD.Value = "";
        lblPassengers.Value = "";
        lblPolicy.Value = "";
        lblWorkshop.Value = "";

        lblMessage.Text = "";

        ddlPolicyDoc.SelectedIndex = 0;
        ddlPromotion.SelectedIndex = 0;
        ddlPolicyInfo.SelectedIndex = 0;

    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarCoverTypeHSBC.aspx");
    }

    protected void ddlCXCoverageType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        ShowProviderCoverType(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXCoverageType.Text));

        lblNCD.Value = rdoNCD.SelectedItem.ToString();
        lblWorkshop.Value = ddlCXCoverageType.SelectedItem.ToString(); // rdoPlanType.SelectedItem.ToString();
        
        ShowCarInsurance(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), Convert.ToInt32(ddlCXCoverageType.Text));

        if (ddlCXCoverageType.SelectedIndex > 0)
            lblWorkshop.Value = ddlCXCoverageType.SelectedItem.ToString();


        //ShowCarInsurance(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXCoverageType.Text));
    }

    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        rfvAuthCode.Validate();
        if (txtAuthCode.Text.Trim().Length > 0)
        {
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());

            if (CheckAuthCode(_authcode, 100))
            {
                CarBaseValueDB _carbasevaluedb = new CarBaseValueDB();
                CarInsuranceDB _carinsurancedb = new CarInsuranceDB();
                CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();
                
                CarInsurance _carinsurance = new CarInsurance();
                _carinsurance = _carinsurancedb.GetCarInsuranceItemByCover(_providercompanyid, Convert.ToInt32(ddlCXCoverageType.Text));

                CarCoverType _carcovertype = new CarCoverType();
                _carcovertype = _carcovertypedb.GetProviderCoverType(_providercompanyid, Convert.ToInt32(ddlCXCoverageType.Text));

                CarBaseValue _carbasevalue = new CarBaseValue();
                _carbasevalue = _carbasevaluedb.GetCarBaseValue(_providercompanyid, Convert.ToInt32(ddlCXCoverageType.Text));
               
                bool CIdelete = _carinsurancedb.Delete(Convert.ToInt32(ddlCXCoverageType.Text), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]));
                bool CBdelete = _carbasevaluedb.Delete(Convert.ToInt32(ddlCXCoverageType.Text), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]));
                bool CCdelete = _carcovertypedb.Delete(Convert.ToInt32(ddlCXCoverageType.Text), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]));

                if (CIdelete && CBdelete && CCdelete)
                {
                    if (_carcovertype != null)
                        InsertProviderAuthTransaction(_carcovertype.ProviderCoverTypeId, "CICarCoverType", 3, 100, _authcode, _carcovertype.ProviderCompanyId, _refceuserid);

                    if (_carinsurance != null)
                        InsertProviderAuthTransaction(_carinsurance.CarInsId, "CICarInsurance", 3, 100, _authcode, _carinsurance.ProviderCompanyId, _refceuserid);

                    if (_carbasevalue != null)
                        InsertProviderAuthTransaction(_carbasevalue.BaseId, "CICarBaseValue", 3, 100, _authcode, _carbasevalue.ProviderCompanyId, _refceuserid);

                    Response.Redirect("ViewProviderCarCoverTypeHSBC.aspx");
                }
                else
                {
                    lblMessage.Text = "Deleting failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }

    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            btnSave.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSave.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
